package org.candan.spider.task.xml;

import java.io.File;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.log4j.Logger;
import org.candan.spider.util.DateUtil;
import org.candan.spider.util.PropertyParse;
import org.candan.spider.util.SLog;


public class XMLProperty {
	
	private static String taskConfig=null;
	private static String planDir=null;


	public static String root = "tasks";
	public static String task = "task";
	public static String status="status";
	public static String id="id";
	public static String name="name";
	public static String url="url";
	public static String output="output";
	public static String startTime = "startTime";
	public static String endTime = "endTime";
	public static String interval = "interval";
	public static String type = "type";
	public static String space = "space";
	public  static String delayTime= "delayTime";
	public  static String lastExecutTime= "lastExecutTime";
	
	public static String plans="plans";
	public static String plan="plan";
	public static String parent="parent";
	
	public static String results="results";
	public static String result="result";
	public static String size="size";
	public static String path = "path";
	
	public static String filter = "filter";
	public static String dataFilter="dataFilter";
	public static String  regex = "regex";
	public static String fromDate="fromDate";
	public static String toDate ="toDate";
	public static String range ="range";
	public static String maxDownload="maxDownload";
	
	public static String tagFilter = "tagFilter";
	public static String filterType = "filterType";
	public static String tag = "tag";
	public static String keywords = "keywords";
	//时间策略类型
	public static String TYPE_HOUR="HOUR";
	public static String TYPE_DAY="DAY";
	public static String TYPE_WEEK="WEEK";
	
	//任务状态
	public static String STATUS_STOP="stop";
	public static String STATUS_BEGIN="begin";
	public static String STATUS_WAIT="wait";
	public static String STATUS_COVER="cover";
	public static String STATUS_FINISH="finish";
	public static String STATUS_OLD="old";
	
	private static Logger ptlogger = new SLog(XMLProperty.class.toString()).getLogger();
	
	 public static String getClassPath()   
	    {   
	        String url = Thread.currentThread().getContextClassLoader()   
	                .getResource("").toString().replace("/", File.separator);   
	        try  
	        {   
	            url = URLDecoder.decode(url, Charset.defaultCharset().toString());   
	            String prefix = "file:".concat(File.separator);   
	            if (url.startsWith(prefix))   
	            {   
	                url = url.substring(prefix.length());   
	            }   
	        }   
	        catch (UnsupportedEncodingException e)   
	        {   
	            ptlogger.error("The system should always have the platform default");   
	        }   
	        return url;   
	    }   
	 public static String getTaskConfig(){
		 if(taskConfig == null){
			 taskConfig = PropertyParse.getValue("preproccess.taskConfig");
			 File f = new File(taskConfig);
			 if(!f.getParentFile().exists()){
				 f.getParentFile().mkdirs();
			 }
		 }
		 return taskConfig;
	 }
	 
	 public static String getPlanDir(){
		 if(planDir == null){
			 planDir = PropertyParse.getValue("preproccess.planDir");
			 if(!new File(planDir).exists()){
				 new File(planDir).mkdirs();
			 }
		 }
		 return planDir;
	 }
	 /**
	  * @category 得到今天计划文件全路径
	 * @return
	 */
	public static String getTodayPlanXML(){
		 return planDir+ DateUtil.getTodayZeroDateStr()+".xml";
	 }
	 /**
	  * @category 得到明天计划文件全路径
	 * @return
	 */
	public static String getTomorrowPlanXML(){
		 Date date = new Date();
		 date.setTime(date.getTime()+DateUtil.oneDayMsec);
		 return planDir+ DateUtil.getTomorrowZeroDateStr()+".xml";
	 }
	 
	 public static void main(String[] args) {
	}

}
